/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Entity - 优惠码
 */
@Entity
@Table(name = "xx_coupon_code")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_coupon_code_sequence")
public class CouponCode extends BaseEntity {

    private static final long serialVersionUID = -1812874037224306719L;

    /**
     * 号码
     */
    private String code;

    /**
     * 是否已使用
     */
    private Boolean isUsed;

    /**
     * 使用日期
     */
    private Date usedDate;

    /**
     * 优惠券
     */
    private Coupon coupon;

    /**
     * 会员
     */
    private Member member;

    /**
     * 订单
     */
    private List<Order> orders = new ArrayList<>();

    /**
     * 获取号码
     *
     * @return 号码
     */
    @Column(nullable = false, updatable = false, unique = true, length = 100)
    public String getCode() {
        return code;
    }

    /**
     * 设置号码
     *
     * @param code 号码
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * 获取是否已使用
     *
     * @return 是否已使用
     */
    @Column(nullable = false)
    public Boolean getIsUsed() {
        return isUsed;
    }

    /**
     * 设置是否已使用
     *
     * @param isUsed 是否已使用
     */
    public void setIsUsed(Boolean isUsed) {
        this.isUsed = isUsed;
    }

    /**
     * 获取使用日期
     *
     * @return 使用日期
     */
    public Date getUsedDate() {
        return usedDate;
    }

    /**
     * 设置使用日期
     *
     * @param usedDate 使用日期
     */
    public void setUsedDate(Date usedDate) {
        this.usedDate = usedDate;
    }

    /**
     * 获取优惠券
     *
     * @return 优惠券
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false, updatable = false)
    public Coupon getCoupon() {
        return coupon;
    }

    /**
     * 设置优惠券
     *
     * @param coupon 优惠券
     */
    public void setCoupon(Coupon coupon) {
        this.coupon = coupon;
    }

    /**
     * 获取会员
     *
     * @return 会员
     */
    @ManyToOne(fetch = FetchType.LAZY)
    public Member getMember() {
        return member;
    }

    /**
     * 设置会员
     *
     * @param member 会员
     */
    public void setMember(Member member) {
        this.member = member;
    }

    /**
     * 获取订单
     *
     * @return 订单
     */
    @OneToMany(mappedBy = "couponCode", fetch = FetchType.LAZY)
    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }





    /**
     * 删除前处理
     */
    @PreRemove
    public void preRemove() {

        if (getOrders() != null) {
            for (Order  order : getOrders()){
                order.setCouponCode(null);
            }

        }
    }

}